package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.YonbipFileUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客商Controller
 *
 * @author ruoyi
 * @date 2025-09-08
 */
@Controller
@RequestMapping("/system/supplier")
public class BdCustSupplierController extends BaseController
{
    private String prefix = "system/supplier";

    private IBdCustSupplierService bdCustSupplierService = SpringUtils.getBean(IBdCustSupplierService.class);
    private IOrgAccbookService orgAccbookService= SpringUtils.getBean(IOrgAccbookService.class);

    private IBdCustclassService  bdCustclassService= SpringUtils.getBean(IBdCustclassService.class);
    private IBdAreaclService  bdAreaclService= SpringUtils.getBean(IBdAreaclService.class);
    private IOrgCqJkService orgCqJkService=SpringUtils.getBean(IOrgCqJkService.class);

    @RequiresPermissions("system:supplier:view")
    @GetMapping()
    public String supplier()
    {
        return prefix + "/supplier";
    }

    /**
     * 查询客商列表
     */
    @RequiresPermissions("system:supplier:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BdCustSupplier bdCustSupplier)
    {
        startPage();
        List<BdCustSupplier> list = bdCustSupplierService.selectBdCustSupplierList(bdCustSupplier);
        return getDataTable(list);
    }

    /**
     * 导出客商列表
     */
    @RequiresPermissions("system:supplier:export")
    @Log(title = "客商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BdCustSupplier bdCustSupplier)
    {
        List<BdCustSupplier> list = bdCustSupplierService.selectBdCustSupplierList(bdCustSupplier);
        ExcelUtil<BdCustSupplier> util = new ExcelUtil<BdCustSupplier>(BdCustSupplier.class);
        return util.exportExcel(list, "客商数据");
    }

    /**
     * 新增客商
     */
    @RequiresPermissions("system:supplier:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客商
     */
    @RequiresPermissions("system:supplier:add")
    @Log(title = "客商", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BdCustSupplier bdCustSupplier)
    {
        return toAjax(bdCustSupplierService.insertBdCustSupplier(bdCustSupplier));
    }

    /**
     * 修改客商
     */
    @RequiresPermissions("system:supplier:edit")
    @GetMapping("/edit/{pkCustSup}")
    public String edit(@PathVariable("pkCustSup") String pkCustSup, ModelMap mmap)
    {
        BdCustSupplier bdCustSupplier = bdCustSupplierService.selectBdCustSupplierByPkCustSup(pkCustSup);
        mmap.put("bdCustSupplier", bdCustSupplier);
        return prefix + "/edit";
    }

    /**
     * 修改保存客商
     */
    @RequiresPermissions("system:supplier:edit")
    @Log(title = "客商", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BdCustSupplier bdCustSupplier)
    {
        return toAjax(bdCustSupplierService.updateBdCustSupplier(bdCustSupplier));
    }

    /**
     * 删除客商
     */
    @RequiresPermissions("system:supplier:remove")
    @Log(title = "客商", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bdCustSupplierService.deleteBdCustSupplierByPkCustSups(ids));
    }
    public void ApiQuerySupplier(){
        try
        {

//            List<OrgAccbook> orgAccbooklist= orgAccbookService.selectOrgAccbookList(new OrgAccbook());
//
//            for(OrgAccbook orgAccbook :orgAccbooklist){
//                OrgCqJk orgCqJk=orgCqJkService.selectOrgCqJkByCode(orgAccbook.getCODE());
//                if(StringUtils.isNull(orgCqJk)){
//                    continue;
//                }
            List<String> lines=new ArrayList<>();
            List<String> linesSb=new ArrayList<>();
            //成功数
            int cnum=0;
            //失败数
            int snum=0;
            BdCustSupplier bdCustSupplier=new BdCustSupplier();
            //  bdCustSupplier.setPkGroup(orgAccbook.getPkGroup());
            List<BdCustSupplier> list = bdCustSupplierService.selectBdCustSupplierList(bdCustSupplier);

            for(BdCustSupplier bdCustSupplier1: list){
                //地区信息
//                    BdAreacl bdAreacl=bdAreaclService.selectBdAreaclByPkAreacl(bdCustSupplier1.getPkAreacl());
//                    //分类信息
                BdCustclass bdCustclass=bdCustclassService.selectBdCustclassByPkCustclass(bdCustSupplier1.getPkCustclass());
//                if(StringUtils.isNull(bdAreacl)){
//                    snum++;
//                    continue;
//                 }
                if(StringUtils.isNull(bdCustclass)){
                    snum++;
                    continue;
                }
                if("~".equals(bdCustSupplier1.getTAXPAYERID())){
                    continue;
                }
                String COMPANYORPERSON="";
                if(bdCustSupplier1.getNAME().length() >= 5 && bdCustSupplier1.getNAME().contains("阳光采购")){
                    COMPANYORPERSON="1";
                }else if(bdCustSupplier1.getNAME().length() >= 5 && bdCustSupplier1.getTAXPAYERID().length()==8){
                    COMPANYORPERSON="2";
                } else if(bdCustSupplier1.getNAME().length() >= 5 && !"~".equals(bdCustSupplier1.getTAXPAYERID())){
                    COMPANYORPERSON="0";
                }else if(bdCustSupplier1.getNAME().length() <5 && !"~".equals(bdCustSupplier1.getTAXPAYERID())){
                    COMPANYORPERSON="1";
                }
                cnum++;
                lines.add(bdCustSupplier1.getCODE()+"|"+bdCustSupplier1.getNAME()+"|"+bdCustclass.getCODE()+"|"+bdCustclass.getNAME()
                        +"|"+"0001"+"|"+"山西省太原市"+"|"+COMPANYORPERSON+"|"+"0107"+"|"+"CNY"+"|"+"MY"+"|"+toAllUpperCase(bdCustSupplier1.getTAXPAYERID())+"|"+bdCustSupplier1.getCUSTENABLESTATE()) ;
            }
            YonbipFileUtils.createFileWithLines(
                    "C:/basedata",
                    "20251104"+"-"+"0107"+"-supplier.txt",
                    lines,
                    false
            );

            linesSb.add("客商数据导入成功"+cnum+"条");
            linesSb.add("客商数据导入失败"+snum+"条");
            YonbipFileUtils.createFileWithLines(
                    "C:/basedata",
                    "20251104"+"-"+"0107"+"-supplier.txt.ok",
                    linesSb,
                    false
            );

            // }


        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static String toAllUpperCase(String str) {
        if (str == "~") {
            return "";
        }
        return str.toUpperCase();
    }
}
